package cn.edu.neusoft.amicooldemo;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.menu.ActionMenuItemView;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Toast;

import cn.edu.neusoft.amicooldemo.bean.Constants;
import cn.edu.neusoft.amicooldemo.iface.CollectIface;
import cn.edu.neusoft.amicooldemo.iface.CollectListener;
import cn.edu.neusoft.amicooldemo.iface.FocusIface;
import cn.edu.neusoft.amicooldemo.iface.FocusListener;
import cn.edu.neusoft.amicooldemo.model.CollectModel;
import cn.edu.neusoft.amicooldemo.model.FocusModel;

public class ViewprojectActivity extends AppCompatActivity implements FocusListener,CollectListener {
    private WebView webView;
    private int resid;
    private int idolid;
    private String SessionID;
    private SharedPreferences sp;
    private int collect;
    private int focus;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewproject);
        focus=0;
        collect=0;
        resid  = getIntent().getIntExtra("resid",1);
        idolid = getIntent().getIntExtra("userid",7);
        sp = getSharedPreferences("amicool_user", MODE_PRIVATE);
        SessionID=sp.getString("SessionID","dfrcg3211kjb1vbe3ibgftkfa6");
        webView = (WebView)findViewById(R.id.webview_project);
        webView.loadUrl(Constants.SERVERADDRESS+"project.php/show/index/id/"+resid);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.view, menu);
        resid  = getIntent().getIntExtra("resid",1);
        idolid = getIntent().getIntExtra("userid",7);
        CollectModel.getInstance().exists("project",resid,SessionID,this);
        FocusModel.getInstance().exists("userfocus",idolid,SessionID,this);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        switch (id){
            case R.id.action_collect:
                if(collect==0){

                    CollectModel.getInstance().collect("project",resid,SessionID,this);
                }
                else{
                    CollectModel.getInstance().uncollect("project",resid,SessionID,this);
                }
                return true;
            case R.id.action_focus:
                if(focus==0){
                    FocusModel.getInstance().focus("userfocus",idolid,SessionID,this);
                }
               /* else{
                    FocusModel.getInstance().unfocus("userfocus",idolid,SessionID,this);
                }*/
                return true;
            default:
                //return super.onOptionsItemSelected(item);
                return true;

        }
    }

    @Override
    public void onResponse(String msg) {
        ActionMenuItemView menu = (ActionMenuItemView)findViewById(R.id.action_collect);
        switch (msg){
            case "1":
                //判断是否已收藏
                menu.setTitle("收藏");
                collect=0;
                break;
            case "3":
                //收藏返回处理
                menu.setTitle("取消收藏");
                collect=1;
                break;
            default:
                //取消收藏返回处理
                menu.setTitle("收藏");
                collect=0;
                break;


        }


    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFocusResponse(String msg) {
        ActionMenuItemView menu = (ActionMenuItemView)findViewById(R.id.action_focus);
        switch (msg){
            case "1":
                menu.setTitle("关注");
                focus=0;
                break;

            case "3":
                menu.setTitle("已关注");
                focus=1;
                break;
            default:
                //取消关注返回处理focus
                menu.setTitle("已关注");
                focus=1;
                break;

        }
    }

    @Override
    public void onFocusFail(String msg) {
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }
}
